// 导入 Element Plus 中英文语言包
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import en from 'element-plus/es/locale/lang/en'
import defaultSettings from '@/settings'
import { store } from '@/store'
import { SidebarStatusEnum } from '@/enums/SidebarStatusEnum'
import { DeviceEnum } from '@/enums/DeviceEnum'
// setup
export const useAppStore = defineStore('app', () => {
    // state
    const size = useStorage('size', defaultSettings.size)
    const language = useStorage('language', defaultSettings.language)
    const sidebarStatus = useStorage('sidebarStatus', SidebarStatusEnum.CLOSED)
    const sidebar = reactive({
        opened: sidebarStatus.value === SidebarStatusEnum.OPENED,
        withoutAnimation: false,
    })
    const device = useStorage('device', DeviceEnum.DESKTOP)
    // mix布局的activePath
    const activeTopMenuPath = useStorage('activeTopMenuPath', '')
    /**
     * 根据语言标识读取对应的语言包
     */
    const locale = computed(() => {
        if (language?.value == 'en') {
            return en
        } else {
            return zhCn
        }
    })
    // actions
    /**
     * 切换语言
     */
    function changeLanguage(val: string) {
        language.value = val
    }
    // 是否开启水印
    const watermarkEnabled = useStorage<boolean>(
        'watermarkEnabled',
        defaultSettings.watermarkEnabled
    )
    /**
     * 侧边栏的枚举值
     * 侧边栏的展示与否
     */
    function closeSideBar() {
        sidebar.opened = false
        sidebarStatus.value = SidebarStatusEnum.CLOSED
    }
    /**
     * 混合模式顶部切换
     */
    function activeTopMenu(val: string) {
        activeTopMenuPath.value = val
    }
    /**
     * 侧边栏的展示与否
     */
    function toggleSidebar() {
        sidebar.opened = !sidebar.opened
        sidebarStatus.value = sidebar.opened
            ? SidebarStatusEnum.OPENED
            : SidebarStatusEnum.CLOSED
    }
    /**
     * 侧边栏打开
     */
    function openSideBar() {
        sidebar.opened = true
        sidebarStatus.value = SidebarStatusEnum.OPENED
    }
    /**
     * 组件大小
     */
    function changeSize(val: string) {
        size.value = val
    }
    /**
     *
     * @param val 设置设备
     */
    function toggleDevice(val: string) {
        device.value = val
    }
    return {
        language,
        locale,
        changeLanguage,
        watermarkEnabled,
        size,
        sidebar,
        device,
        closeSideBar,
        activeTopMenuPath,
        activeTopMenu,
        toggleSidebar,
        openSideBar,
        changeSize,
        toggleDevice,
    }
})
export function useAppStoreHook() {
    return useAppStore(store)
}
